package com.supermap.dataservice.attention.dao;

import com.supermap.dubbo.api.attention.entity.Attention;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author lxs
 * @Title:我的关注
 * @Description:数据访问层
 * @date 2018/2/27  11:55
 */
@Mapper
public interface AttentionDao {

    /**
     *添加我的关注物
     * @param attention
     * @return
     */
    @InsertProvider(type=AttentionSql.class,
            method="insertt_AttentionSql")
    int saveAttention(@Param("attention") Attention attention);

    /**
     * 根据当前登录用户名得到对应的id
     * @param loginName
     * @return
     */
    @Select("select id from t_user  where loginName=#{loginName}")
    int getUserIdByuserName(@Param("loginName") String loginName);

    /**
     * 查询用户的关注物
     * @param userid
     * @return
     */
    @Select(" select ts.* from t_s_scan_details ts INNER JOIN t_user t on ts.userid=t.id where ts.userid=#{userid} ")
    List<Attention> getAttentionsByUserId(@Param("userid") int userid);

    /**
     * 修改作物
     * @param attention
     * @return
     */
    @UpdateProvider(type=AttentionSql.class,
                    method="updateAttentionById_sql")
    int updateAttentionById(@Param("attention") Attention attention);

    /**
     * 删除关注的作物
     */
    @Delete("delete  from t_s_scan_details where id=#{id} ")
    int deleteByCropId(@Param("id") int id);

    /**
     * 内部类 编写动态sql
     * @author lxs
     *
     */
    public class AttentionSql{

        public String insertt_AttentionSql(Map<String, Object> map){
            Attention attention = (Attention)map.get("attention");
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO t_s_scan_details (cropid,areaid,userid,areaname,cropname) VALUES ");
            sql.append("('"+ attention.getCropid() +"','"+ attention.getAreaid()+"',"+ attention.getUserid()+"");
            sql.append(",'"+ attention.getAreaname()+"','"+ attention.getCropname()+"')");
            return sql.toString();
        }

        public String updateAttentionById_sql(Map<String, Object> map){
            Attention attention = (Attention)map.get("attention");
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE t_s_scan_details SET ");
            sql.append(" cropid = " + "'"+attention.getCropid()+"',");
            sql.append(" areaid = " + "'"+attention.getAreaid()+"',");
            sql.append(" areaname = " + "'"+attention.getAreaname()+"',");
            sql.append(" cropname = " + "'"+attention.getCropname()+"'");
            sql.append(" WHERE id = " + attention.getId() );
            return sql.toString();
        }
    }
}
